package com.ruoyi.health.service.impl;

import java.util.List;

import com.hy.corecode.idgen.YitIdGenerator;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.health.domain.Family;
import com.ruoyi.health.mapper.UserMapper;
import com.ruoyi.health.domain.User;
import com.ruoyi.health.service.IUserService;

/**
 * 平台用户 Service业务层处理
 * 
 * @author dalian
 * @date 2023-08-06
 */
@Service
public class UserServiceImpl implements IUserService 
{
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private YitIdGenerator yitIdGenerator;  //雪花生成id （https://gitee.com/super-lin/yitter-idgenerator-spring-boot-starter）

    /**
     * 查询平台用户 
     * 
     * @param id 平台用户 主键
     * @return 平台用户 
     */
    @Override
    public User selectUserById(Long id)
    {
        return userMapper.selectUserById(id);
    }

    @Override
    public User selectUserByPhone(String phone) {
        return userMapper.selectUserByPhone(phone);
    }

    /**
     * 查询平台用户 列表
     * 
     * @param user 平台用户 
     * @return 平台用户 
     */
    @Override
    public List<User> selectUserList(User user)
    {
        return userMapper.selectUserList(user);
    }

    /**
     * 新增平台用户 
     * 
     * @param user 平台用户 
     * @return 结果
     */
    @Transactional
    @Override
    public int insertUser(User user)
    {
        user.setCreateTime(DateUtils.getNowDate());
        user.setId(yitIdGenerator.next());  //生成雪花id
        int rows = userMapper.insertUser(user);
        insertFamily(user);
        return rows;
    }

    /**
     * 修改平台用户 
     * 
     * @param user 平台用户 
     * @return 结果
     */
    @Transactional
    @Override
    public int updateUser(User user)
    {
        user.setUpdateTime(DateUtils.getNowDate());

        return userMapper.updateUser(user);
    }



    /**
     * 批量删除平台用户 
     * 
     * @param ids 需要删除的平台用户 主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteUserByIds(Long[] ids)
    {
        //删除关联的家人信息
        userMapper.deleteFamilyByUserIds(ids);
        //删除关联的关注
        userMapper.deleteConcernByUserIds(ids);
        //TODO:删除关联的收藏

        return userMapper.deleteUserByIds(ids);
    }

    /**
     * 删除平台用户 信息
     * 
     * @param id 平台用户 主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteUserById(Long id)
    {
        userMapper.deleteFamilyByUserId(id);
        return userMapper.deleteUserById(id);
    }

    /**
     * 查询手机号是否已经注册
     * @param phone
     */
    @Override
    public int checkPhoneUnique(String phone) {
        if(StringUtils.isNotNull(userMapper.selectUserByPhone(phone))){
            return 1;  //已注册
        }
        return 0;  //手机号未注册
    }

    /**
     * 管理员给前台用户重置密码
     * @param id
     * @return
     */
    @Override
    public int resetPassword(Long id) {
        User user = new User();
        user.setId(id);
        //使用ruoyi提供的SecurityUtils给密码加密
        user.setPassword(SecurityUtils.encryptPassword("12345678"));

        return userMapper.updatePassword(user);
    }

    @Override
    public int updateUserPassword(Long id,String newPwd) {
        User user = new User();
        user.setId(id);
        //使用ruoyi提供的SecurityUtils给密码加密
        user.setPassword(SecurityUtils.encryptPassword(newPwd));
        return userMapper.updatePassword(user);
    }

    @Override
    public String selectUserPassword(Long id) {
        User user = userMapper.selectUserPasswordById(id);
        if(StringUtils.isNull(user) || StringUtils.isEmpty(user.getPassword())){
            throw new ServiceException("获取用户密码信息失败！");
        }
        return user.getPassword();
    }

    /**
     * 新增我的家人信息
     * 
     * @param user 平台用户 对象
     */
    public void insertFamily(User user)
    {
        List<Family> familyList = user.getFamilyList();
        Long id = user.getId();
        if (StringUtils.isNotNull(familyList))
        {
            List<Family> list = new ArrayList<Family>();
            for (Family family : familyList)
            {
                family.setUserId(id);
                list.add(family);
            }
            if (list.size() > 0)
            {
                userMapper.batchFamily(list);
            }
        }
    }
}
